package log;


import java.util.Iterator;

import storage.file.*;
import static storage.file.Block.*;

public class LogIterator implements Iterator<BasicLogRecord>{
	private BlockID blockID;
	private Block block = new Block();
	private int currentrec;
	
	LogIterator(BlockID blockID) {
		this.blockID = blockID;
		block.read(blockID);
		currentrec = block.getInt(LogManager.LAST_POS);
	}

	public boolean hasNext() {
		return currentrec > 0 || blockID.offset() > 0;
	}
	
	public BasicLogRecord next() {
		if (currentrec == 0) moveToNextBlock();
		currentrec = block.getInt(currentrec);
		return new BasicLogRecord(block, currentrec + INT_SIZE);
	}
	
	public void remove() {
		throw new UnsupportedOperationException();
	}
	
	private void moveToNextBlock() {
		blockID = new BlockID(blockID.fileName(), blockID.offset() - 1);
		block.read(blockID);
		currentrec = block.getInt(LogManager.LAST_POS);
	}
}
